/**
 * Copyright (c) Salomon Automation GmbH
 */
package ro.cuzma.larry.bookscollection.md;

import java.util.ArrayList;
import java.util.List;

import ro.cuzma.larry.bookscollection.persistance.xml.AuthorHelper;
import ro.cuzma.larry.bookscollection.persistance.xml.BookHelper;
import ro.cuzma.larry.bookscollection.persistance.xml.LibraryHelper;
import ro.cuzma.larry.persistance.common.Entity;
import ro.cuzma.larry.persistance.common.EntityIdAndValue;
import ro.cuzma.larry.persistance.xml.XMLSimpleIdValue;
import ro.cuzma.larry.persistance.xml.XMLTagValue;

public class EntityFactory {

    public static EntityIdAndValue buildSimpleIdValue(XMLSimpleIdValue obj) {
        EntityIdAndValue retObj = new EntityIdAndValue();
        retObj.setId(obj.getId().getValue());
        retObj.setValue(obj.getValueTag().getValue());
        return retObj;

    }

    public static Author buildAuthor(AuthorHelper ah) {
        Author author = new Author();
        author.setId(ah.getId().getLong());
        author.setName(ah.getName().getValue());
        author.setFirstName(ah.getFirstName().getValue());
        author.setMiddleName(ah.getMiddleName().getValue());
        return author;

    }

    public static Book buildBook(BookHelper bh) {
        Book book = new Book();
        book.setId(bh.getId().getLong());
        book.setName(bh.getName().getValue());
        List<Author> authList = new ArrayList<Author>();
        Author author;
        for (XMLTagValue ent : bh.getAuthors().getData()) {
            author = new Author();
            author.setId(ent.getLong());
            authList.add(author);
        }
        book.getAuthors().setEntityList(authList);
        if (bh.getLanguageID() != null && bh.getLanguageID().getValue() != null) {
            EntityIdAndValue<String> lang = new EntityIdAndValue<String>();
            lang.setId(bh.getLanguageID().getValue());
            book.setLanguage(lang);
        }
        if (bh.getAquireDate() != null && bh.getAquireDate().getValue() != null) {
            book.setAquireDate(bh.getAquireDate().getDate());
        }
        return book;
    }

    public static Library buildLibrary(LibraryHelper lh) {
        Library library = new Library();
        library.setName(lh.getName().getValue());
        library.setRoot(lh.getRoot().getValue());
        // Authors
        for (AuthorHelper ah : lh.getAuthors().getData()) {
            library.addAuthor(buildAuthor(ah));
        }
        // Languages
        for (XMLSimpleIdValue obj : lh.getLanguages().getData()) {
            library.getLanguages().add(buildSimpleIdValue(obj));
        }
        // Types
        for (XMLSimpleIdValue obj : lh.getFormats().getData()) {
            library.getFormats().add(buildSimpleIdValue(obj));
        }

        // Formats
        for (XMLSimpleIdValue obj : lh.getTypes().getData()) {
            library.getTypes().add(buildSimpleIdValue(obj));
        }

        // Series
        Entity<String> series;
        for (XMLTagValue ent : lh.getSeries().getData()) {
            series = new Entity<String>();
            series.setId(ent.getValue());
            library.getSeries().add(series);
        }

        // Books
        // List<Book> books = new ArrayList<Book>();
        Book book;
        for (BookHelper bookNode : lh.getBooks().getData()) {
            book = EntityFactory.buildBook(bookNode);
            // connect with authors
            List<Author> lst = new ArrayList<Author>();
            for (Author author : book.getAuthors().getEntityList()) {
                lst.add(library.getAuthors().getEntityById(author.getId()));
            }
            book.getAuthors().setEntityList(lst);
            // connect with language
            if (book.getLanguage() != null) {
                book.setLanguage(library.getLanguages().getEntityById(
                        book.getLanguage().getId().toString()));
            }
            library.addBook(book);
        }
        return library;
    }
}
